package org.gruposp2p.proyectokarma.gwt.client.ui.document;

import java.util.HashSet;
import java.util.Set;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.gruposp2p.proyectokarma.gwt.client.widget.ClickableLabel;

public class AddOptionsDialog implements ClickableLabel.Handler {

    private static AddOptionsDialogUiBinder uiBinder = GWT
    	.create(AddOptionsDialogUiBinder.class);

    interface AddOptionsDialogUiBinder extends
    	UiBinder<Widget, AddOptionsDialog> { }
	
    @UiField DialogBox dialogBox;
    @UiField VerticalPanel mainPanel;
    @UiField VerticalPanel inputPanelsContainer;
    @UiField VerticalPanel optionPanelContainer;
    @UiField Button addOptionButton;
    @UiField TextArea optionTextArea;
    @UiField Button acceptButton;
    @UiField Button cancelButton;
    
    Handler dialogHandler; 
    Set<String> optionSet;

    public AddOptionsDialog() {
        uiBinder.createAndBindUi(this);
        SubmitHandler sh = new SubmitHandler();
        acceptButton.setVisible(false);
        acceptButton.setStylePrimaryName("acceptButton");
        cancelButton.setStylePrimaryName("cancelButton");
        addOptionButton.setStylePrimaryName("addButton");
        optionTextArea.addKeyDownHandler(sh);
    }

    public void show(Handler handler) {
    	this.dialogHandler = handler;
        //dialogBox.setPopupPosition((Window.getClientWidth()/4), 40);
        optionSet = new HashSet<String>();
        optionPanelContainer.clear();
        acceptButton.setVisible(false);
        dialogBox.center();
        dialogBox.show();
    }
   
    @UiHandler("acceptButton")
    void handleAcceptButton(ClickEvent e) {
    	dialogHandler.updateOptionsPanel(optionSet);
		dialogBox.hide();
    }
    
    @UiHandler("addOptionButton")
    void handleTagsButton(ClickEvent e) {
    	String option = optionTextArea.getText().trim();
    	if ("".equals(option)) return;
    	optionSet.add(option);	
    	optionTextArea.setText("");
    	showSelectedOptions();
    }
    
	private void showSelectedOptions () {
		optionPanelContainer.clear();
    	for (String option : optionSet) {
    		ClickableLabel optionLabel = new ClickableLabel(
    				option, this, ClickableLabel.Type.OPTION);
    		optionPanelContainer.add(optionLabel);
    	}
        acceptButton.setVisible(optionSet.size() > 0);
	}
    
    @UiHandler("cancelButton")
    void handleCancelButton(ClickEvent e) {
    	dialogBox.hide();
    }
 
    private class SubmitHandler implements KeyDownHandler {
		@Override
		public void onKeyDown(KeyDownEvent event) {
			if (KeyCodes.KEY_ENTER == event.getNativeKeyCode()) {
				acceptButton.click();
			}		
		}
	}

    public interface Handler {
    	void updateOptionsPanel(Set<String> optionSet);
    }


	@Override
	public void processIconClick(ClickableLabel clickableLabel) {
		optionSet.remove(clickableLabel.getText());
		showSelectedOptions();
	}
    
}